package socket;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;
import java.util.Scanner;

public class ClientSuper {
    public static void main(String[] args) throws IOException, InterruptedException {
            //创建套接字
            Socket sk = new Socket(InetAddress.getLocalHost(), 11111);

            //发送数据
            byte[] bytes = new byte[1024];
            int len;

            //获取输出,输出流
            Scanner sc = new Scanner(System.in);

            System.out.println("请选择选项   1 进入聊天室   2 传送文件到服务端   3 退出");
            String nextInt = sc.nextLine();

            while (true) {
                if (nextInt.equals("1")||nextInt.equals("2")||nextInt.equals("3")) {

                    if (nextInt.equals("1")) {

                        OutputStream os = sk.getOutputStream();

                        //写数据

                        os.write("1".getBytes());

                        while (true) {

                            System.out.println();
                            System.out.println("系统:请发送消息给服务端" + new Date());
                            InputStream in = System.in;
                            os = sk.getOutputStream();

                            //写数据
                            len = in.read(bytes);

                            os.write(bytes, 0, len);

                            System.out.println("系统:等待服务端消息" + new Date());

                            InputStream is = sk.getInputStream();
                            byte[] bytes1 = new byte[1024];
                            int read = is.read(bytes1);

                            String back = new String(bytes1, 0, read);
                            System.out.println("系统:接收到服务端消息 = " + back + new Date());
                            System.out.println("---------");

                            System.out.println("请选择下一步操作   1 继续聊天   2 传送文件到服务端  3 退出");
                            nextInt = sc.nextLine();
                            if (!nextInt.equals("1")) {
                                break;
                            }
                            os.write("1".getBytes());
                        }

                    }

                    if (nextInt.equals("2")) {

                        OutputStream os = sk.getOutputStream();

                        //写数据

                        os.write("2".getBytes());

                        System.out.println("请输入文件的地址(绝对地址,必须是文本类型)!");
                        String address = sc.nextLine();
                        FileInputStream fis = new FileInputStream(address);
                        byte[] fileBytes = new byte[1024];
                        int fileLen;
                        while ((fileLen = fis.read(fileBytes)) != -1) {
                            //传给服务器
                            os.write(fileBytes, 0, fileLen);
                        }
                        //todo

                        //获取传送文件的服务器返回结果
                        InputStream is = sk.getInputStream();
                        byte[] filebytes = new byte[1024];
                        int read = is.read(filebytes);

                        System.out.println("服务端端消息 : "+new String(filebytes, 0, read));
                        //关流
                        fis.close();

                        System.out.println("请选择下一步操作   1 继续聊天   2 传送文件到服务端  3 退出");
                        nextInt = sc.nextLine();
                    }

                    if (nextInt.equals("3")) {

                        OutputStream os = sk.getOutputStream();

                        //写数据

                        os.write("3".getBytes());

                        sk.shutdownOutput();
                        break;
                    }
                }else {
                    System.out.println("请输入正确的选择!!!");
                    System.out.println("请选择选项   1 进入聊天室   2 传送文件到服务端   3 退出");
                    nextInt = sc.nextLine();
                }
            }
            //关闭
            // 套接字以及输出流,接受服务器通知

            System.out.println("您已经退出聊天室");
            sk.close();
        }
    }

